<?php
/**
 * QuestionController
 *
 * PHP Version 5.4
 *
 * @author liuxuTJ <liuxuphp@icesmart.cn>
 */
namespace Admin\Controller;

use Admin\Service\BaseService;
use Admin\Service\PermissionService;
use Admin\Service\UserService;
use Model\PaperModel;
use Model\QuestionModel;
use Model\QuestionTagModel;
use Think\Controller;

/**
 * Class QuestionController
 */
class QuestionController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $userService = new UserService();
        $userInfo = $userService->validateLogin();   //验证是否登录

        $permissionService = new PermissionService();
        $permission = $permissionService->validate($userInfo['userId'], [
            'tagAdd' => '/' . CONTROLLER_NAME . '/tagAdd',
            'tagDel' => '/' . CONTROLLER_NAME . '/tagDel',
            'tagEdit' => '/' . CONTROLLER_NAME . '/tagEdit',
            'questionAdd' => '/' . CONTROLLER_NAME . '/questionAdd',
            'questionDel' => '/' . CONTROLLER_NAME . '/questionDel',
            'questionEdit' => '/' . CONTROLLER_NAME . '/questionEdit',
        ]);
        $menu = $permissionService->menu($userInfo['userId']);

        $this->assign('permission', $permission);
        $this->assign('menu', $menu);
        $this->assign('userInfo', $userInfo);
    }

    public function index(){
        $model = new QuestionModel();
        $tagModel = new QuestionTagModel();
        $tagList = $tagModel->select();
        $page = $this->page($model, null);
        $show = $page->show();
        $limit[] = $page->firstRow;
        $limit[] = $page->listRows;
        $list = $model->findByFilter(null, ['id' => 'DESC'], $limit);
        $this->assign("tagList",$tagList);
        $this->assign('list', $list);
        $this->assign("page", $show);
        $this->display();
    }

    /**
     * 添加题库
     */
    public function questionAdd(){
        $model = new QuestionModel();
        $data = $model->create();
        $image = $_FILES['image'];
        $baseService = new BaseService();
        $data['image'] = $baseService->upload($image);

        if(empty($data['title'])){
            $this->error("标题不可为空","",1);
        }
        if(empty($data['score'])){
            $this->error("分值不可空","",1);
        }
        if(empty($data["option".$data['answer']])){
            $this->error("至少有一个正确选项要填写","",2);
        }
        $where['title'] = $data['title'];
        $findQuestion = $model->where($where)->find();

        if(!empty($findQuestion)){
            $this->error("此题已存在！",'',1);
        }
        $add = $model->add($data);
        if ($add) {
            $this->success('添加成功', '/Admin/Question', 1);
        } else {
            $this->error('添加失败','',1);
        }

    }

    /**
     * 题库删除
     */
    public function  questionDel(){
        $id = intval(I("get.id"));
        if (empty($id) || $id == 0) {
            $this->error("非法参数！",'',1);
        }
        $paperModel = new PaperModel();
        $where['titles'] = array('like',",%".$id."%");
        $fingPaper = $paperModel->where($where)->find();
        if(!empty($fingPaper)){
            $this->error("此题已被试卷{$fingPaper['name']}占用！",'',2);
        }
        $model = new QuestionModel();
        $delete = $model->delete($id);
        if ($delete) {
            $this->success('删除成功', '/Admin/Question', 1);
        } else {
            $this->error('删除失败','',1);
        }
    }

    public  function questionEdit(){
        $id = intval(I("get.id"));
        if (empty($id) || $id == 0) {
            $this->error("非法参数！",'',1);
        }
        $tagModel = new QuestionTagModel();
        $tagList = $tagModel->select();
        $model = new QuestionModel();
        $data = $model->get($id);
        $this->assign("tagList",$tagList);
        $this->assign("data", $data);
        $this->display();
    }

    public function modifyQuestion(){
        $model = new QuestionModel();
        $data = $model->create();
        $image = $_FILES['image'];
        if (!empty($image['name'])) {
            $baseService = new BaseService();
            $image = $baseService->upload($image);
            $data['image'] = $image;
        }
        if(empty($data['title'])){
            $this->error("标题不可为空","",1);
        }
        if(empty($data['score'])){
            $this->error("分值不可空","",1);
        }
        if(empty($data["option".$data['answer']])){
            $this->error("至少有一个正确选项要填写","",2);
        }

        $add = $model->save($data);
        if ($add) {
            $this->success('修改成功', '/Admin/Question', 1);
        } else {
            $this->error('修改失败','',1);
        }
    }
    /**
     * 标签列表
     */
    public function tag()
    {
        $model = new QuestionTagModel();
        $page = $this->page($model, null);
        $show = $page->show();
        $limit[] = $page->firstRow;
        $limit[] = $page->listRows;
        $list = $model->findByFilter(null, ['id' => 'DESC'], $limit);
        $this->assign('list', $list);
        $this->assign("page", $show);
        $this->display();
    }

    /**
     * 添加题库标签
     */
    public function tagAdd()
    {

        $model = new QuestionTagModel();
        $data = [
            'name' => I("post.name")
        ];
        if ($data['name'] == "") {
            $this->error("非法参数",'',1);
        };
        $where['name'] = $data['name'];
        $findTag = $model->where($where)->find();

        if(!empty($findTag)){
            $this->error("标签已存在！",'',1);
        }
        $add = $model->add($data);
        if ($add) {
            $this->success('添加成功', '/Admin/Question/tag', 1);
        } else {
            $this->error('添加失败','',1);
        }
    }

    /**
     * 删除题库标签
     */
    public function tagDel()
    {

        $id = intval(I("get.id"));
        if (empty($id) || $id == 0) {
            $this->error("非法参数！",'',1);
        }
        $questionModel = new QuestionModel();
        $findQuestion = $questionModel->where("tagId = {$id}")->find();
        if(!empty($findQuestion)){
            $this->error("此标签被题库使用，无法删除",'',1);
        }
        $model = new QuestionTagModel();
        $delete = $model->delete($id);
        if ($delete) {
            $this->success('删除成功', '/Admin/Question/tag', 1);
        } else {
            $this->error('删除失败','',1);
        }
    }

    /**
     * 修改标签默认数据
     */
    public function tagEdit()
    {
        $id = intval(I("get.id"));
        if (empty($id) || $id == 0) {
            $this->error("非法参数！",'',1);
        }
        $model = new QuestionTagModel();
        $data = $model->get($id);
        $this->assign("data", $data);
        $this->display();
    }

    public function modifyTag()
    {
        $id = intval(I("post.id"));
        $name = I("post.name");
        if (empty($id) || $id == 0) {
            $this->error("非法参数！",'',1);
        }
        if (empty($name)) {
            $this->error("非法参数！",'',1);
        }
        $model = new QuestionTagModel();
        $where['name'] = $name;
        $findTag = $model->where($where)->find();

        if(!empty($findTag)){
            $this->error("标签已存在！",'',1);
        }
        $data['name'] = $name;

        $saveTag = $model->where("id = ".$id."")->save($data);

        if($saveTag){
            $this->success('修改成功', '/Admin/Question/tag', 1);
        }else{
            $this->error('修改失败','',1);
        }
    }

}